package Ajax;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.http.HttpServletRequest;

public class CertificatePage extends AbsPersonalPage {

	public CertificatePage(String id, HttpServletRequest request) {
		super(id, request);
		mStrSubClassName = "resumecertificate";
	}

	@Override
	public void execSql(Statement stmt, HttpServletRequest request)
			throws SQLException {
		if (bCreate) {
			return;
		}

		if (bSubmit) {

			String sql = "INSERT INTO certificate VALUES(null, " + mResumeId + ", "
					+ (request.getParameter("property") == null ? "false" : "true") + ", "
					+ request.getParameter("dsyear") + ", "
					+ request.getParameter("dsmonth") + ", "
					+ request.getParameter("cera") + ", "
					+ request.getParameter("cerb") + ", '"
					+ request.getParameter("dcerc") + "', '"
					+ request.getParameter("grade") + "')";

			if (nModify > 0) {
				sql = "UPDATE certificate SET "
					+ "cert_prop = " + (request.getParameter("property") == null ? "false" : "true")
					+ ", syear = " + request.getParameter("dsyear")
					+ ", smonth = " + request.getParameter("dsmonth")
					+ ", dcera = " + request.getParameter("cera")
					+ ", dcerb = " + request.getParameter("cerb")
					+ ", dcerc = '" + request.getParameter("dcerc")
					+ "', grade = '" + request.getParameter("grade")
					+ "' WHERE id = " + nModify;
			}

			stmt.executeUpdate(sql);

			return;
		}

		if (bDelete) {
			String sql = "DELETE FROM certificate WHERE id = " + mId;

			stmt.executeUpdate(sql);

			return;
		}

		String sql = "SELECT certificate.id, certificate.syear, certificate.smonth, certificate.cert_prop, certificate.dcera, certificate.dcerb, certificate.dcerc, certificate.grade, "
				   + "cate_cera.id, cate_cera.list_name, cate_cerb.id, cate_cerb.list_name FROM certificate LEFT "
				   + "JOIN cate_cera ON certificate.dcera = cate_cera.id "
				   + "JOIN cate_cerb ON certificate.dcerb = cate_cerb.id WHERE ";

		if (nModify > 0) {
			sql += "certificate.id = " + nModify;
		} else {
			sql += "certificate.resume_id = " + mResumeId;
		}

		rs = stmt.executeQuery(sql);
	}

	@Override
	protected void writeWithoutCreate(PrintWriter writer) throws SQLException {
		if (rs == null) {
			return;
		}

		writer.print("<ol><li>属性: 公开, 您的简历可以被全部企业用户查看</li><li>属性: 隐藏, 企业用户不可以查看您的简历详情</li>"
				+ "</ol>"
				+ "<div class=\"topic_list\">"
				+ "<table cellpadding=\"0\" cellspacing=\"0\">"
				+ "<thead>"
				+ "<tr><td>属性</td><td>获证时间</td><td>证书类别</td><td>获证成绩</td><td>管理选项</td></tr>"
				+ "</thead>");

		int i = 0;

		while (rs.next()) {
			boolean is_public = rs.getBoolean("cert_prop");
			String cer = rs.getString("dcerc");
			String grade = rs.getString("grade");

			writer.println("<tr class=\"bg" + (++i % 2) + "\">");
			writer.println("<td><img src=\"./images/personal/property_"
					+ (is_public ? "y" : "n") + ".gif\" alt=\"属性: "
					+ (is_public ? "公开" : "隐藏") + "\" /></td>");
			writer.println("<td>" + rs.getString("syear") + "-" + rs.getString("smonth") + "</td>");

			if (cer == null || cer.length() < 1) {
				writer.println("<td>" + rs.getString("dcerb") + "</td>");
			} else {
				writer.println("<td>" + cer + "</td>");
			}

			if (grade == null || grade.length() < 1) {
				writer.println("<td>-</td>");
			} else {
				writer.println("<td>" + grade + "</td>");
			}

			writer.println("<td><a href=\"#\" onclick=\"resumecertificate('','','"
					+ rs.getString("id") + "');return false;\">修改</a> | ");
			writer.println("<a href=\"#\" onclick=\"if (confirm('您是否确定要删除该条教育经历\t')){resumecertificate('','','','"
					+ rs.getString("id")
					+ "');return false;}else{return false;}\">删除</a></td>");
		}

		writer.println("</tr></table></div><table cellpadding=\"0\" cellspacing=\"0\" class=\"formtable\"><tr><th width=\"100\">&nbsp;</th>	<td>");
		writer.println("<input type=\"button\" name=\"create\" value=\"添加证书\" onclick=\"resumecertificate('',true)\" class=\"submit\" />");
		writer.println("<input type=\"button\" name=\"nextstep\" value=\"跳到下一步\" onclick=\"resumeload('attachment')\" class=\"button\" />");
		writer.println("</td></tr></table><div id=\"resumextentform\"></div>");
	}

	@Override
	protected void writeWithCreate(PrintWriter writer) throws IOException {
		String form_title = "<form action=\"ResumeSys?action=personal&amp;option=resume&amp;extent=certificate&amp;info=" + mResumeId + "&amp;submit=1\" ";
		File file = new File("c:\\xml\\certificate.xml");

		if (file.exists()) {
			InputStream fs = new FileInputStream(file);
			BufferedReader br = new BufferedReader(new InputStreamReader(fs,
					"utf-8"));

			String Line = null;

			writer.print(form_title);

			while ((Line = br.readLine()) != null) {
				writer.println(Line);
			}

			br.close();
			fs.close();
		} else {
			file.createNewFile();
		}
	}

	@Override
	protected void writeModify(PrintWriter writer) throws SQLException {
		if (rs == null || !rs.next()) {
			return;
		}
		
		boolean is_public = rs.getBoolean("certificate.cert_prop");

		writer.print("<form action=\"ResumeSys?action=personal&amp;option=resume&amp;extent=certificate&amp;info=" + mResumeId + "&amp;submit=1\" ");
		writer.print("name=\"wane_resume\" method=\"post\" target=\"ajaxform\" onsubmit=\"return resumecertificate();\"><table cellpadding=\"0\" cellspacing=\"0\" class=\"formtable\" style=\"margin-botton:30px;\">");
		writer.print("<caption><h2>证书设置 - 修改</h2></caption><tr><th width=\"100\"><span class=\"warn\">*</span> 获证时间:</th>");

		writer.print("<td><a href=\"#\" onclick=\"category.load('tsyear','stimey',document.wane_resume.syear.value);return false;\" class=\"select\"><span id=\"tsyear\">" + rs.getInt("certificate.syear") + "</span></a>");
		writer.print("	<input type=\"hidden\" id=\"syear\" name=\"syear\" value=\"" + rs.getInt("certificate.syear") + "\" /><input type=\"hidden\" id=\"dsyear\" name=\"dsyear\" value=\"" + rs.getInt("certificate.syear") + "\" />");
		writer.print("<a href=\"#\" onclick=\"category.load('tsmonth','stimem',document.wane_resume.smonth.value);return false;\" class=\"select\"><span id=\"tsmonth\">" + rs.getInt("certificate.smonth") + "</span></a>");
		writer.print("<input type=\"hidden\" id=\"smonth\" name=\"smonth\" value=\"" + rs.getInt("certificate.smonth") + "\" /><input type=\"hidden\" id=\"dsmonth\" name=\"dsmonth\" value=\"" + rs.getInt("certificate.smonth") + "\" /></td></tr>");

		writer.print("<tr><th><span class=\"warn\">*</span> 属性:</th><td>");
		writer.print("<input type=\"checkbox\" name=\"property\" value=\"1\" ");

		if (is_public) {
			writer.print("checked");
		}

		writer.print(" /> 属性: 公开</td></tr><tr><th><span class=\"warn\">*</span> 证书类别:</th><td>");
		writer.print("<a href=\"#\" onclick=\"category.load('tcera','cera',document.wane_resume.cera.value);return false;\" class=\"select\"><span id=\"tcera\">" + rs.getString("cate_cera.list_name") + "</span></a>");
		writer.print("<input type=\"hidden\" id=\"cera\" name=\"cera\" value=\"" + rs.getInt("cate_cera.id") + "\" /><input type=\"hidden\" id=\"dcera\" name=\"dcera\" value=\"" + rs.getString("cate_cera.list_name") + "\" />");
		writer.print("<a href=\"#\" onclick=\"category.load('tcerb','cerb',document.wane_resume.cera.value);return false;\" class=\"select\"><span id=\"tcerb\">" + rs.getString("cate_cerb.list_name") + "</span></a>");
		writer.print("<input type=\"hidden\" id=\"cerb\" name=\"cerb\" value=\"" + rs.getInt("cate_cerb.id") + "\" /><input type=\"hidden\" id=\"dcerb\" name=\"dcerb\" value=\"" + rs.getString("cate_cerb.list_name") + "\" />");
		writer.print(" 找不到您的证书类别, 请手工填写<br /><input type=\"text\" name=\"dcerc\" class=\"t_input\" size=\"35\" value=\"" + rs.getString("certificate.dcerc") + "\" /></td></tr>");
		writer.print("<tr><th>获证成绩:</th><td><input type=\"text\" name=\"grade\" class=\"t_input\" size=\"15\" value=\"" + rs.getString("certificate.grade") + "\" /></td></tr><tr><th>&nbsp;</th><td>");

		writer.print("<input type=\"hidden\" name=\"modifid\" value=\"" + nModify + "\"><input type=\"hidden\" name=\"modifysubmit\" value=\"1\" />");
		writer.print("<input type=\"submit\" name=\"Submit\" value=\"保存修改\" class=\"submit\" /><input type=\"button\" name=\"ClickBack\" value=\"取消操作\" onclick=\"resumeload('certificate');\" class=\"button\" /></td></tr></table></form>");
	}
}
